<?php
/**
 * XIE--Scorpion
 *
 * 用户操作
 */
namespace App\Models\Admin;

use DB,Session,Redirect;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
	protected $table = 'admin_user';
    public $timestamps = false;

	/*
	 *登录操作
	 */
	static function login($user,$pwd)
	{
        
        $u_list=DB::table('admin_user')->where('admin_user',$user)->get();
        if($u_list){
        	$pwd=md5($pwd);
        	$p_list=DB::table('admin_user')->where('admin_pwd',$pwd)->get();
        	if($p_list){
        		Session::put('admin_id',$u_list[0]->admin_id);
                //存储权限路径
                $acts=DB::table('admin_role_power')->join('admin_power','admin_power.admin_pid','=','admin_role_power.admin_pid')->where('admin_role_power.admin_rid',$u_list[0]->admin_rid)->where('admin_power_level','!=','0')->get();
                foreach($acts as $v){
                    $arr[]=$v->admin_power_path;
                }
                Session::put('admin_power',$arr);
                $nums=DB::table('admin_role_power')->join('admin_power','admin_power.admin_pid','=','admin_role_power.admin_pid')->where('admin_role_power.admin_rid',$u_list[0]->admin_rid)->get();
                Session::put('num',$nums);
        		return array('1','登录成功');
        	}else{
        		//登录失败
        		return array('0','密码错误');
        	}

        }else{
        	//用户名不存在
        	return array('0','用户名不存在');
        }
	}

	/*
	 *修改密码
	 */
	static function checkPwd($old_pwd,$new_pwd)
	{
        $admin_id=Session::get('admin_id','0');
        if($admin_id=='0'){
        	return '0';
        }

        $u_list=DB::table('admin_user')->where('admin_id',$admin_id)->get();
        
        if($u_list[0]->admin_pwd==$old_pwd){
            DB::table('admin_user')->where('admin_id',$admin_id)->update(array('admin_pwd'=>$new_pwd));
            return '1';
        }else{
        	return '0';
        }
        
	}
}